<?php
class frontend_menu_block extends block {
  
  protected $items_html = "";
  
  private function render_items($pages) {
    $module = request::get_module_name();
    $html = ""; 
    if (count($pages)<=1)
      $pages = array($pages);    
    
    foreach ($pages as $page) {
      $page_name  = trim($page["@attributes"]["name"]);
      $page_title = phrase::__(trim($page["@attributes"]["title"]));
      $url = $module."_".$page_name;
      $target = "_self";
      
      //check if has specific target for anchor tag
      if (isset($page["@attributes"]["target"])) 
        $target = $page["@attributes"]["target"];
      
      //check if has specific url
      if (isset($page["@attributes"]["url"])) 
        $url = $page["@attributes"]["url"];
      
      //check if is a muted menu item
      if (isset($page["@attributes"]["muted"]))
        $muted = strtolower(trim($page["@attributes"]["muted"]));
      else
        $muted = "false";            
      if ($muted=="true") {
        $url = "#";
        $target = "_self";
      }
      
      //before child items
      $html .= 
      "<li>\r\n".
      "  <a href='$url' target='$target'>$page_title</a>\r\n".
      "  <ul>\r\n";
      
      //child items
      if (isset($page["page"]))
        $html .= $this->render_items($page["page"]);
      
      //after child items
      $html .=
      "  </ul>\r\n".
      "</li>\r\n";
    }
    return $html;
  }
  
  public function prepare() {
    $module_info = system::get_module_info();
    $menu = $module_info["menu"];
    $this->items_html = $this->render_items($menu["page"]);
  }
  
  public function finish() {
  }
}

